import Axios from 'axios'
const Notification = (opt) => {
    console.log(opt.message)
}


Axios.defaults.baseURL = 'https://wise.scaotu.cn'



const http = ({ url, data = {}, method = 'get' }) => {
    const m = method.toLowerCase()
    const config = {
        method: m,
        url: url,
    }

    if (m === 'get' || m === 'delete') {
        config.params = data
    } else {
        config.data = data
    }
    return new Promise((resolve, reject) => {
        Axios(config)
            .then(res => {
                if (res && res.data) {
                    return res.data
                } else {
                    reject()
                }
            })
            .then(data => {
                if (data.Succeed === true) {
                    return resolve(data)
                } else {
                    Notification({
                        title: '提示',
                        message: data.msg || '出错了',
                        type: 'error'
                    });
                    if (data.msg === '请重新登录') {
                        router.push('/')
                    }
                    return reject(data.msg)
                }
            }).catch(err => {
                Notification({
                    title: '提示',
                    message: '网络错误',
                    type: 'error'
                });
            })
    })
}

export default http
